//获取应用实例
var req = require('../../interfaceImpl.js');
var config = require('../../interfaceConfig.js');
var app = getApp();
var datas ="";
Page({
  data: {
    // 页面配置 
    winWidth: 0,
    winHeight: 0,
    // tab切换
    currentTab: 0,
  },
  // 滚动切换标签样式
  switchTab: function (e) {
    this.setData({
      currentTab: e.detail.current
    });
    this.checkCor();
  },
  // 点击标题切换当前页时改变样式
  clickTab: function (e) {
    var cur = e.target.dataset.current;
    if (this.data.currentTab == cur) { return false; }
    else {
      this.setData({
        currentTab: cur
      })
    }
  },
  //判断当前滚动超过一屏时，设置tab标题滚动条。
  checkCor: function () {
    if (this.data.currentTab > 4) {
      this.setData({
        scrollLeft: 300
      })
    } else {
      this.setData({
        scrollLeft: 0
      })
    }
  },
  onLoad: function () {
    console.log(app.globalData.userinfo);
    console.log(app.globalData.loginuserinfo)
    if (app.globalData.userinfo == '') {
      //头像 名字 余额 积分 
      this.setData({
        head: '../../resources/header_icon.png',
        name: app.globalData.loginuserinfo.mobile
      })
    }
    else {
      this.setData({
        head: app.globalData.userinfo.avatarUrl,
        name: app.globalData.userinfo.nickName,
        vipType: app.globalData.loginuserinfo.vipType
      })
    }
    var that = this;
    //  高度自适应
    wx.getSystemInfo({
      success: function (res) {
        var clientHeight = res.windowHeight
        var rpxR = res.pixelRatio;
        var calc = (clientHeight - 180 / rpxR)*rpxR
        console.log(calc)
        that.setData({
          winHeight: calc
        });
      }
    });
    return req.MyCouponList2(app.globalData.token).then(res => {
      datas = res.data.data;
      console.log(res.data.data)
      this.setData({ datas: res.data.data, winHeight: 236 * res.data.data.NotUseCoupons.length })
    })
  },

  // // 滑动切换tab
  swiperTab: function (e) {
    var height= 0;
      var that = this;
      switch (e.detail.current){
        case 0:
          height = 236 *datas.NotUseCoupons.length;
        break;
        case 1:
          height = 236 * datas.WaitUseCoupons.length;
          break;
        case 2:
          height = 236 * datas.UsedCoupons.length;
          break;
        case 3:
          height = 236 * datas.ExpiresCoupons.length;
          break;
      }
      that.setData({ currentTab: e.detail.current, winHeight:height });
  },
  // // 点击tab切换
  // clickTab: function (e) {
  //     var that = this;
  //     if (this.data.currentTab === e.target.dataset.current) {
  //         return false;
  //     } else {
  //         that.setData({
  //             currentTab: e.target.dataset.current
  //         })
  //     }
  // },

  viewDetail: function (e) {
    var pojo = e.currentTarget.dataset.pojo
    wx.navigateTo({
      url: 'couponDetail?pojo=' + JSON.stringify(pojo) + ''

    });
  },

  hasshiyong: function () {
    wx.showModal({
      title: '提示',
      content: '该卷已经使用',
    })
  },
  hasguoqi: function () {
    wx.showModal({
      title: '提示',
      content: '该卷已经过期',
    })
  },
  footerTap: app.footerTap
})
